<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.code" clearable class="filter-item" style="width: 200px;" placeholder="请输入诊断编码" />
      <el-input v-model="listQuery.name" clearable class="filter-item" style="width: 200px;" placeholder="请输入诊断名称" />
      <el-input v-model="listQuery.standardCode" clearable class="filter-item" style="width: 200px;" placeholder="请输入诊断标准编码" />
      <el-button v-permission="['GET /admin/diagnosis/list']" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter" >查找</el-button>
      <el-button v-permission="['POST /admin/diagnosis/create']" class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
      <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" label="ID" prop="id" sortable width="120"/>

      <el-table-column align="left" label="编码" prop="code" width="180"/>
      <el-table-column align="left" label="诊断名称" prop="name" />
      <el-table-column align="left" label="标准编码" prop="standardCode" width="140" />
      <el-table-column align="left" label="性别限制" width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.genderRestrict == 1">限男性</el-tag>
          <el-tag type="danger" v-else-if="scope.row.genderRestrict == 2">限女性</el-tag>
          <el-tag type="success" v-else>不限</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" class-name="small-padding fixed-width" width="220">
        <template slot-scope="scope">
          <el-button v-permission="['GET /admin/diagnosis/drug/list']" type="primary" size="mini" @click="handleDrugList(scope.row)">用药</el-button>
          <el-button v-permission="['POST /admin/diagnosis/update']" type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-permission="['POST /admin/diagnosis/delete']" type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { listDiagnosis, deleteDiagnosis } from "@/api/diagnosis"
import Pagination from "@/components/Pagination"

export default {
  name: "DiagnosisList",
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        code: undefined,
        name: undefined,
        standardCode: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      listDiagnosis(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.$router.push({ path: '/diagnosis/create' })
    },
    handleUpdate(row) {
      this.$router.push({ path: '/diagnosis/edit', query: { id: row.id }})
    },
    handleDrugList(row) {
      this.$router.push({ path: '/diagnosis/drug', query: { id: row.id }})
    },
    handleDelete(row) {
      this.$confirm('删除诊断, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDiagnosis(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除诊断成功'
            })
            this.getList()
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.data.errmsg
            })
          })
      }).catch()
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['ID', '编码', '诊断名称', '标准码']
        const filterVal = ['id', 'code', 'name', 'standardCode']
        excel.export_json_to_excel2(
          tHeader,
          this.list,
          filterVal,
          '诊断信息'
        )
        this.downloadLoading = false
      })
    }
  }
}
</script>

<style scoped>

</style>
